﻿using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities;

namespace Sustainalytics.Portfolios.BL.Tests
{
    [TestClass]
    public class PortfoliosManagementTests
    {
        private IRepositoryCollection<Portfolio> PortfolioRepo { get; set; }
        private IRepositoryCollection<PortfolioDetails> PortfolioDetailsRepo { get; set; }
        private IPortfolioManager PortfolioManager { get; set; }

        [TestInitialize]
        public void Initialize()
        {
            //Create repository
            this.PortfolioRepo = new RepositoryCollection<Portfolio>(ConfigurationManager.ConnectionStrings["ClientPlatform_Test"].ConnectionString);
            this.PortfolioDetailsRepo = new RepositoryCollection<PortfolioDetails>(ConfigurationManager.ConnectionStrings["ClientPlatform_Test"].ConnectionString);

            //PortfolioTestingData.CleanupPortfolioRepo(this.PortfolioRepo);
            //PortfolioTestingData.CleanupPortfolioDetailsRepo(this.PortfolioDetailsRepo);

            //PortfolioTestingData.InitializeRepo(this.PortfolioRepo);
            //PortfolioTestingData.InitializeRepo(this.PortfolioDetailsRepo);

            //Create BL
            this.PortfolioManager = new PortfolioManager(this.PortfolioRepo, this.PortfolioDetailsRepo, new RepositoryCollection<PortfolioUserInput>(ConfigurationManager.ConnectionStrings["ClientPlatform_Test"].ConnectionString));
        }

        //[TestMethod]
        //public void DeleteFullPortfolio_Authorized()
        //{
        //    PortfolioUserClaims userClaims = new PortfolioUserClaims();
        //    userClaims.UserName = "ileana.popescu@sustainalytics.com";
        //    userClaims.UserFullName = "Ileana Popescu";
        //    userClaims.UserId = new Guid("4387c2a7-1eda-11e4-92ce-6c3be5be9f98");
        //    userClaims.AccountId = new Guid("528ea769-2d17-e411-92ce-6c3be5be9f98");

        //    Guid id = Guid.Parse("00000000-0000-0000-0000-" + "010".PadLeft(12, '0'));
        //    this.PortfolioManager.DeletePortfolio(id, userClaims);

        //    Portfolio portfolio = this.PortfolioRepo.Read(id);

        //    Assert.IsTrue(portfolio == null, "Portfolio should be deleted");
        //}

       

        //[TestMethod]
        //public void UpdateShared_Authorized()
        //{
        //    PortfolioUserClaims userClaims = new PortfolioUserClaims();
        //    userClaims.UserName = "ileana.popescu@sustainalytics.com";
        //    userClaims.UserFullName = "Ileana Popescu";
        //    userClaims.UserId = new Guid("4387c2a7-1eda-11e4-92ce-6c3be5be9f98");
        //    userClaims.AccountId = new Guid("528ea769-2d17-e411-92ce-6c3be5be9f98");

        //    Guid id = Guid.Parse("00000000-0000-0000-0000-" + "010".PadLeft(12, '0'));

        //    PortfolioSharingStatus status = new PortfolioSharingStatus();
        //    status = PortfolioSharingStatus.Private;
        //    this.PortfolioManager.UpdatePortfolioStatus(id, status, userClaims);
        //    Portfolio portfolio = this.PortfolioRepo.Read(id);

        //    Assert.IsTrue(portfolio.SharingStatus == (int)status, "Portfolio should be private");

        //    status = PortfolioSharingStatus.Shared;
        //    this.PortfolioManager.UpdatePortfolioStatus(id, status, userClaims);
        //    Portfolio portfolio2 = this.PortfolioRepo.Read(id);

        //    Assert.IsTrue(portfolio2.SharingStatus == (int)status, "Portfolio should be shared");
        //}

        //[TestMethod]
        //[ExpectedException(typeof(PortfolioSharedUnauthorizedException))]
        //public void UpdateShared_Unauthorized()
        //{
        //    PortfolioUserClaims userClaims = new PortfolioUserClaims();
        //    userClaims.UserName = "ileana@sustainalytics.com";
        //    userClaims.UserFullName = "Ileana";
        //    userClaims.UserId = new Guid("1387c2a7-1eda-11e4-92ce-6c3be5be9f98");
        //    userClaims.AccountId = new Guid("528ea769-2d17-e411-92ce-6c3be5be9f98");

        //    var status = PortfolioSharingStatus.Private;

        //    Guid id = Guid.Parse("00000000-0000-0000-0000-" + "011".PadLeft(12, '0'));
        //    this.PortfolioManager.UpdatePortfolioStatus(id, status, userClaims);
        //}

        //[TestMethod]
        //public void UpdateName_Authorized()
        //{
        //    PortfolioUserClaims userClaims = new PortfolioUserClaims();
        //    userClaims.UserName = "ileana.popescu@sustainalytics.com";
        //    userClaims.UserFullName = "Ileana Popescu";
        //    userClaims.UserId = new Guid("4387c2a7-1eda-11e4-92ce-6c3be5be9f98");
        //    userClaims.AccountId = new Guid("528ea769-2d17-e411-92ce-6c3be5be9f98");

        //    string newName = "New name";
        //    Guid id = Guid.Parse("00000000-0000-0000-0000-" + "010".PadLeft(12, '0'));

        //    this.PortfolioManager.UpdatePortfolioName(id, newName, userClaims);
        //    Portfolio portfolio = this.PortfolioRepo.Read(id);

        //    Assert.IsTrue(portfolio.Name == newName, "Portfolio name is not correct!");
        //    Assert.IsTrue(portfolio.NameLowercase == newName.ToLower(), "Portfolio name is not correct");
        //}

        //[TestMethod]
        //[ExpectedException(typeof(PortfolioModificationUnauthorizedException))]
        //public void UpdateName_Unauthorized()
        //{
        //    PortfolioUserClaims userClaims = new PortfolioUserClaims();
        //    userClaims.UserName = "ileana@sustainalytics.com";
        //    userClaims.UserFullName = "Ileana";
        //    userClaims.UserId = new Guid("1387c2a7-1eda-11e4-92ce-6c3be5be9f98");
        //    userClaims.AccountId = new Guid("528ea769-2d17-e411-92ce-6c3be5be9f98");

        //    Guid id = Guid.Parse("00000000-0000-0000-0000-" + "011".PadLeft(12, '0'));
        //    string newName = "New name";

        //    this.PortfolioManager.UpdatePortfolioName(id, newName, userClaims);
        //}

        //[TestMethod]
        //[ExpectedException(typeof(PortfolioNameAlreadyExists))]
        //public void UpdateName_NameExists()
        //{
        //    PortfolioUserClaims userClaims = new PortfolioUserClaims();
        //    userClaims.UserName = "ileana.popescu@sustainalytics.com";
        //    userClaims.UserFullName = "Ileana Popescu";
        //    userClaims.UserId = new Guid("4387c2a7-1eda-11e4-92ce-6c3be5be9f98");
        //    userClaims.AccountId = new Guid("528ea769-2d17-e411-92ce-6c3be5be9f98");

        //    string newName = "Portfolio test 1";
        //    Guid id = Guid.Parse("00000000-0000-0000-0000-" + "010".PadLeft(12, '0'));

        //    this.PortfolioManager.UpdatePortfolioName(id, newName, userClaims);
        //}
    }
}
